//
//  ZKSwipeDetailCell.m
//  ZAKER
//
//  Created by Anyfish002 on 16/3/14.
//  Copyright © 2016年 yaohongchao. All rights reserved.
//

#import "ZKSwipeDetailCell.h"


@interface ZKSwipeDetailCell ()
@property(nonatomic,weak)  UIImageView *imgV;
@property(nonatomic,weak)  UIImageView *imgVContent;
@property(nonatomic,weak)  UIView *cellContentView;
@property(nonatomic,strong)  NSMutableArray *views;
@property(nonatomic,strong)  NSMutableArray *bgViews;
@end




@implementation ZKSwipeDetailCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];{
        [self setCellHead];
        [self setContent];
    }
    return self;
}

-(void)setCellHead{
    UIImageView *imgV = [[UIImageView alloc]init];
    [self.contentView addSubview:imgV];
    imgV.contentMode = UIViewContentModeScaleAspectFit;
    imgV.clipsToBounds = YES;
//    WS(weakSelf);
    imgV.frame = CGRectMake(0, 20, CGRectGetWidth(self.bounds), 80);
//    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(weakSelf.contentView).offset(20);
//        make.left.right.equalTo(weakSelf.contentView);
//        make.height.mas_equalTo(80);
//    }];
    self.imgV = imgV;
    UIView *cellContentView = [[UIView alloc]init];
    [self.contentView addSubview:cellContentView];
    self.cellContentView = cellContentView;
    cellContentView.frame = CGRectMake(0, CGRectGetMaxY(imgV.frame), CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds)-CGRectGetMaxY(imgV.frame));

//    [cellContentView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.equalTo(weakSelf.contentView);
//        make.top.equalTo(imgV.mas_bottom);
//    }];
}


-(void)setContent{
    self.views = [NSMutableArray array];
    for (NSInteger i = 0; i < 6; i++) {
        ZKSwipeDetailCellView *view = [[ZKSwipeDetailCellView alloc]init];
        [self.views addObject:view];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewClick:)];
        [view addGestureRecognizer:tap];
        [self.cellContentView addSubview:view];
    }
}

-(void)viewClick:(UITapGestureRecognizer*)recongnize{
    if(self.blockClick){
        self.blockClick(((ZKSwipeDetailCellView*)recongnize.view).article);
    }
}

-(void)refresh:(ZKSwipeDetailPages*)pageData{
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:pageData.diy.bgimage_url]];
    WS(weakSelf);
    int postionImg = 0,imgDataIndex = 0;
    for (int i = 0; i < pageData.arrayArticle.count; i++) {
        imgDataIndex = i;
        ZKSwipeDetailArticles *article = [pageData.arrayArticle objectWithIndex:i];
        if(article.thumbnail_mpic.length > 0 || article.media.count > 0){
            NSString *strTail = [article.pk substringWithRange:NSMakeRange(article.pk.length-8, 8)];
            postionImg = [self numberWithHexString:strTail] % 4;
            break;
        }
        
    }
    NSMutableDictionary *dict = @{@"0":@0,@"1":@1,@"2":@2,@"3":@3}.mutableCopy;
    [dict removeObjectForKey:[NSString stringWithFormat:@"%d",postionImg]];
    int index = [pageData.tpl_style intValue] % 3;
    int postionLong = [[dict.allKeys objectWithIndex:index] intValue];
    [dict removeObjectForKey:[NSString stringWithFormat:@"%d",postionLong]];
    
    for (NSInteger i = 0; i < self.bgViews.count; i++) {
        UIView *view = [self.bgViews objectWithIndex:i];
        for (UIView *subView in view.subviews) {
            [subView removeFromSuperview];
        }
        [view mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.cellContentView);
            if(i == 0){
                make.top.equalTo(weakSelf.cellContentView);
//                UIView *view1 = [weakSelf.bgViews objectWithIndex:i];
//                make.bottom.equalTo(view1.mas_top);
                make.height.equalTo(weakSelf.cellContentView).multipliedBy(i == postionImg ? 0.4 : 0.2);
            }
            else if(i == 3){
                make.bottom.equalTo(weakSelf.cellContentView.mas_bottom);
                make.height.equalTo(weakSelf.cellContentView).multipliedBy(i == postionImg ? 0.4 : 0.2);
            }else{
                UIView *viewBefore = [weakSelf.bgViews objectWithIndex:i-1];
                make.top.equalTo(viewBefore.mas_bottom);
                make.height.equalTo(weakSelf.cellContentView).multipliedBy(i == postionImg ? 0.4 : 0.2);
            }
        }];
    }

    NSMutableArray *articles = [NSMutableArray arrayWithArray:pageData.arrayArticle];
    UIView *bgImageView = [self.bgViews objectWithIndex:postionImg];
    ZKSwipeDetailCellView *imgView = [self.views objectWithIndex:0];
    [bgImageView addSubview:imgView];
    [imgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(bgImageView);
    }];
    imgView.isShowImage = YES;
    imgView.article = [articles objectWithIndex:imgDataIndex];
    [articles removeObjectAtIndex:imgDataIndex];
    
    UIView *bgLongView = [self.bgViews objectWithIndex:postionLong];
    ZKSwipeDetailCellView *longView = [self.views objectWithIndex:1];
    [bgLongView addSubview:longView];
    [longView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.left.right.equalTo(bgLongView);
    }];
    longView.isShowImage = NO;
    longView.article = [articles objectWithIndex:0];
    if(articles.count > 0){
        [articles removeObjectAtIndex:0];
    }

    index = 0;
    for (NSInteger i = 0; i < self.bgViews.count; i++) {
        if(i != postionImg && i != postionLong){
            UIView *bgView = [self.bgViews objectWithIndex:i];
            ZKSwipeDetailCellView *firstView = [self.views objectWithIndex:2*index + 2];
            ZKSwipeDetailCellView *seconView = [self.views objectWithIndex:2*index + 3];
            [bgView addSubview:firstView];
            [bgView addSubview:seconView];
            [firstView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(bgView);
                make.width.equalTo(bgView).multipliedBy(0.5);
            }];
            [seconView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.top.bottom.equalTo(bgView);
                make.left.equalTo(bgView.mas_centerX);
            }];
            firstView.article = [articles objectWithIndex:0];
            if(articles.count > 0){
                [articles removeObjectAtIndex:0];
            }
            firstView.isShowImage = NO;
            seconView.article = [articles objectWithIndex:0];
            if(articles.count > 0){
                [articles removeObjectAtIndex:0];
            }            firstView.isShowImage = NO;
            index++;
        }
    }
}

-(void)refreshWithFrmae:(ZKSwipeDetailPages*)pageData{
    [self.imgV sd_setImageWithURL:[NSURL URLWithString:pageData.diy.bgimage_url]];
    int postionImg = 0,imgDataIndex = 0;
    for (int i = 0; i < pageData.arrayArticle.count; i++) {
        imgDataIndex = i;
        ZKSwipeDetailArticles *article = [pageData.arrayArticle objectWithIndex:i];
        if(article.thumbnail_mpic.length > 0 || article.media.count > 0){
            NSString *strTail = [article.pk substringWithRange:NSMakeRange(article.pk.length-8, 8)];
            postionImg = [self numberWithHexString:strTail] % 4;
            break;
        }
    }
    NSMutableDictionary *dict = @{@"0":@0,@"1":@1,@"2":@2,@"3":@3}.mutableCopy;
    [dict removeObjectForKey:[NSString stringWithFormat:@"%d",postionImg]];
    int index = [pageData.tpl_style intValue] % 3;
    int postionLong = [[dict.allKeys objectWithIndex:index] intValue];
    [dict removeObjectForKey:[NSString stringWithFormat:@"%d",postionLong]];

    CGFloat height = CGRectGetHeight(self.cellContentView.frame)/5;
    CGFloat width = CGRectGetWidth(self.cellContentView.frame);

    NSMutableArray *articles = [NSMutableArray arrayWithArray:pageData.arrayArticle];

    
    CGRect frame = CGRectMake(0, postionImg*height, width, height*2);
    ZKSwipeDetailCellView *imgDetailView = [self.views objectWithIndex:0];
    imgDetailView.frame = frame;
    imgDetailView.isShowImage = YES;
    imgDetailView.article = [articles objectWithIndex:imgDataIndex];
    [articles removeObjectAtIndex:imgDataIndex];

    
    frame = CGRectMake(0,
                       postionLong > postionImg ? height*(postionLong+1) : height*postionLong,
                       width,
                       height);
    ZKSwipeDetailCellView *longDetailView = [self.views objectWithIndex:1];
    longDetailView.frame = frame;
    longDetailView.isShowImage = NO;
    longDetailView.article = [articles objectWithIndex:0];
    [articles removeObjectAtIndex:0];

    index = 0;
    for (NSInteger i = 0; i < 4; i++) {
        if(i != postionImg && i != postionLong){
            ZKSwipeDetailCellView *firstView = [self.views objectWithIndex:2*index + 2];
            ZKSwipeDetailCellView *seconView = [self.views objectWithIndex:2*index + 3];
            CGFloat y = 0;
            int viewIndex = [[dict objectForKey:[dict.allKeys firstObject]] intValue];
            [dict removeObjectForKey:[dict.allKeys firstObject]];
            if(viewIndex > postionImg){
                y = (viewIndex + 1) * height;
            }else{
                y = viewIndex*height;
            }
            CGRect frame = CGRectMake(0, y, width/2, height);
            firstView.frame = frame;
            firstView.isShowImage = NO;
            firstView.article = [articles objectWithIndex:0];
            if(articles.count > 0){
                [articles removeObjectAtIndex:0];
            }
            frame = CGRectMake(width/2, y, width/2, height);
            seconView.frame = frame;
            seconView.isShowImage = NO;
            seconView.article = [articles objectWithIndex:0];
            if(articles.count > 0){
                [articles removeObjectAtIndex:0];
            }
            index++;
        }
    }
}

- (NSInteger)numberWithHexString:(NSString *)hexString{
    
    const char *hexChar = [hexString cStringUsingEncoding:NSUTF8StringEncoding];
    
    int hexNumber;
    
    sscanf(hexChar, "%x", &hexNumber);
    
    return (NSInteger)hexNumber;
}

@end


@interface ZKSwipeDetailCellView()


@end

@implementation ZKSwipeDetailCellView

-(instancetype)init{
    self = [super init];
    if(self){
        [self setup];
    }
    return self;
}

-(void)setup{
    UIImageView *imgV = [UIImageView new];
    imgV.contentMode = UIViewContentModeScaleAspectFill;
    imgV.clipsToBounds = YES;
    [self addSubview:imgV];
    self.imgV = imgV;
    
    UILabel *lblTitle = [UILabel new];
    [self addSubview:lblTitle];
    self.lblTitle = lblTitle;
    lblTitle.font = [UIFont systemFontOfSize:16];
    lblTitle.textColor = [UIColor whiteColor];
    
    UILabel *lblSubTitle = [UILabel new];
    lblSubTitle.font = [UIFont systemFontOfSize:14];
    [self addSubview:lblSubTitle];
    self.lblSubTitle = lblSubTitle;
}


-(void)setArticle:(ZKSwipeDetailArticles *)article{
    _article = article;
    for (UIView *view in self.imgV.subviews) {
        [view removeFromSuperview];
    }
    WS(weakSelf);
    if(self.isShowImage){
        [self.imgV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(weakSelf);
        }];
        UIView *maskView = [[UIView alloc]init];
        maskView.backgroundColor = [UIColor colorWithWholeRed:0 green:0 blue:0 alpha:0.3];
        [self.imgV addSubview:maskView];
        WS(weakSelf);
        [maskView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.equalTo(weakSelf);
        }];
        if(article.thumbnail_mpic.length > 0){
            [self.imgV sd_setImageWithURL:[NSURL URLWithString:article.thumbnail_mpic]];
        }else{
            ZKSwipeDetailMedia *media = [article.media firstObject];
            [self.imgV sd_setImageWithURL:[NSURL URLWithString:media.m_url]];
        }
        self.lblTitle.textColor = [UIColor whiteColor];
        self.lblTitle.numberOfLines = 2;
    }else{
        self.lblTitle.textColor = [UIColor blackColor];
        self.lblTitle.numberOfLines = 3;
    }
    self.lblTitle.text = article.title;
    [self.lblTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf).offset(10);
        make.right.equalTo(weakSelf).offset(-10);
        make.bottom.equalTo(weakSelf);
        make.height.mas_equalTo(weakSelf.lblTitle.numberOfLines*weakSelf.lblTitle.font.pointSize*ScreenScale);
    }];
}

@end















